package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYfindClubInfoReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYclubinfoStruct;
import com.game.server.message.proto.out.XYclubplayerStruct;
import com.game.server.message.proto.out.XYfindClubInfoRsp;
import com.game.server.message.requtil.NoticeUtil;
import com.game.server.system.AbstractRoom;
import com.game.server.system.AbstractTable;

import java.util.ArrayList;
import java.util.Collection;

public class XYfindClubInfoReq extends AbstractXYfindClubInfoReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            XYfindClubInfoRsp rsp = new XYfindClubInfoRsp();
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            if (clubInfo != null) {
                XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (memberInfo != null) {
                    client.setClubid(this.clubid);
                    //NoticeUtil.InformTheClubIndividual(client,clubInfo,memberInfo);
                    XYclubinfoStruct xYclubinfoStruct = new XYclubinfoStruct();
                    xYclubinfoStruct.setId(clubInfo.getId());
                    xYclubinfoStruct.setName(clubInfo.getName());
                    xYclubinfoStruct.setHeadurl(clubInfo.getHeadurl());
                    xYclubinfoStruct.setContent(clubInfo.getContent());
                    xYclubinfoStruct.setCreatorid(clubInfo.getCreatorid());
                    xYclubinfoStruct.setCreatorhead(DataManager.getInstance().getMapUserData().get(clubInfo.getCreatorid()).getHead());
                    xYclubinfoStruct.setCreatorname(DataManager.getInstance().getMapUserData().get(clubInfo.getCreatorid()).getName());
                    xYclubinfoStruct.setCreatedate(clubInfo.getCreatedate().getTime() + "");
                    xYclubinfoStruct.setXingyu(clubInfo.getXingyuvalue());
                    xYclubinfoStruct.setOnlinecount(clubInfo.getOnlineCount());
                    xYclubinfoStruct.setAllPeopleCount(clubInfo.getPlayerCount());
                    xYclubinfoStruct.setClubjijin(clubInfo.getClubjijin());
                    xYclubinfoStruct.setPromotionType(clubInfo.getPromotion_type());
                    xYclubinfoStruct.setUnionid(clubInfo.getUnionid());
                    if (clubInfo.getUnionid() != 0) {
                        XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(clubInfo.getUnionid());
                        if (unionInfo!=null){
                            xYclubinfoStruct.setUnionName(unionInfo.getUnion_name());
                            xYclubinfoStruct.setUnionCreatorCid(unionInfo.getCreator_clubid());
                        }
                    }
                    if (memberInfo.getIsManager()!=3){
                        xYclubinfoStruct.setXingyuvalue(clubInfo.getTotalContribution()+"");
                    }else{
                        xYclubinfoStruct.setXingyuvalue(memberInfo.getXingyuvalue()+"");
                    }
                    xYclubinfoStruct.setPaijuCount(clubInfo.findAllTable(client.getUserId(),clubInfo.getId()).size());
                    XYclubplayerStruct clubplayerStruct = new XYclubplayerStruct();
                    clubplayerStruct.setId(memberInfo.getUid());
                    clubplayerStruct.setXingyuvalue(memberInfo.getXingyuvalue());
                    clubplayerStruct.setIsManager(memberInfo.getIsManager());
                    clubplayerStruct.setIsAccessory(memberInfo.getIsAccessory());
                    rsp.setClubplayer(clubplayerStruct);
                    rsp.setClubinfo(xYclubinfoStruct);

                    //俱乐部红点
                    int notifCount = XyNotificationLogDao.findClubNotifCount(clubInfo.getId());
                    if (notifCount>0){
                        XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
                        alterRsp.setType(7);
                        alterRsp.setOperateclubid(client.getUserId());
                        alterRsp.setClubid(clubInfo.getId());
                        alterRsp.setUnionid(0);
                        client.Send(alterRsp);
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
